package com.project.controller;

import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.PageDomain;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.project.domain.bo.DmProjectTypeBo;
import com.project.domain.entity.SyntProject;
import com.project.service.IProjectTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * 项目分类 Controller
 *
 * @author baige
 * @date 2025-02-08
 */
@Api(tags = "项目分类")
@RestController
@RequestMapping("/project/xmflsz")
public class ProjectTypeController extends BaseController {
    @Autowired
    private IProjectTypeService dmProjectTypeService;

    /**
     * 查询项目分类列列表
     */
    @ApiOperation(value = "查询")
//    @PreAuthorize("@ss.hasPermi('project:xmflsz:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            @ApiParam(value = "项目编号", required = false) @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "项目分类", required = false) @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "级别", required = false) @RequestParam(value = "projectLevelId", required = false) String projectLevelId,
            @ApiParam(value = "项目性质", required = true) @RequestParam(value = "classId", required = true) String classId,
            PageDomain page) {
        return  dmProjectTypeService.selectDmProjectTypeList_haveCount(code, name, projectLevelId,classId,page);
    }


    /**
     * 获取项目分类列详细信息
     */
    @ApiOperation(value = "详情")
//    @PreAuthorize("@ss.hasPermi('project:xmflsz:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(dmProjectTypeService.selectDmProjectTypeByID(id));
    }

    /**
     * 新增分类
     */
    @ApiOperation(value = "新增")
//    @PreAuthorize("@ss.hasPermi('project:xmflsz:add')")
    @Log(title = "项目分类列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmProjectTypeBo dmProjectType) {
        int code = dmProjectTypeService.insertDmProjectType(dmProjectType);
        switch (code){
            case 100:
                return error("分类编号已存在");
            case 200:
                return success();
            case 500:
                return error();
            default:
                return success();
        }
    }

    /**
     * 修改项目分类
     */
    @ApiOperation(value = "修改")
//    @PreAuthorize("@ss.hasPermi('project:xmflsz:edit')")
    @Log(title = "项目分类列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmProjectTypeBo dmProjectType) {
        return toAjax(dmProjectTypeService.updateDmProjectType(dmProjectType));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
//    @PreAuthorize("@ss.hasPermi('project:xmflsz:remove')")
    @Log(title = "项目分类列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id) {
        return toAjax(dmProjectTypeService.deleteDmProjectTypeByID(id));
    }

    /**
     * 获取最大的科目前缀
     */
    @ApiOperation(value = "获取最大的分类编号前缀")
    @GetMapping("/getMaxPrefix")
    public AjaxResult getMaxPrefix(@ApiParam(value = "项目分类,zXProject->纵向,yfContract->横向,xJProject->校级", required = true)
                                   @RequestParam(value = "projectClass", required = true)
                                   @Pattern(regexp = "zXProject|yfContract|xJProject")
                                   String projectClass,
                                   @ApiParam(value = "分类层级", required = true)
                                   @RequestParam(value = "typeLevel", required = true)
                                   String typeLevel,
                                   @ApiParam(value = "upCode", required = false) @RequestParam(value = "upCode", required = false) String upCode) {
        return AjaxResult.success("查询成功",dmProjectTypeService.getMaxPrefix(projectClass,typeLevel,upCode));
    }

    /**
     * 获取引用此分类的项目相亲
     */
    @ApiOperation(value = "获取引用此分类的项目集合")
    @GetMapping("/getUseProjectList")
    public TableDataInfo getUseProjectList(
                                   @ApiParam(value = "id", required = true)
                                   @RequestParam(value = "id", required = true) String id,
                                   @RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize")Integer pageSize) {
       List<SyntProject> useProjectList = dmProjectTypeService.getUseProjectList(id, pageNum, pageSize);
       return   getDataTable(useProjectList);
    }


}
